package com.jb.util;


import java.util.Vector;

import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;


public class ClassTreeModel implements TreeModel {
    private boolean showAncestors;
    private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();

    private Klass rootKlass;

    public ClassTreeModel(Klass root) {
        showAncestors = false;
        rootKlass = root;
    }

    /**
     * Used to toggle between show ancestors/show descendant and
     * to change the root of the tree.
     */
    public void showAncestor(boolean b, Object newRoot) {
        showAncestors = b;
        Klass oldRoot = rootKlass;
        if (newRoot != null) {
            rootKlass = (Klass)newRoot;
        }
        fireTreeStructureChanged(oldRoot);
    }


    //////////////// Fire events //////////////////////////////////////////////

    /**
     * The only event raised by this model is TreeStructureChanged with the
     * root as path, i.e. the whole tree has changed.
     */
    protected void fireTreeStructureChanged(Klass oldRoot) {

        TreeModelEvent e = new TreeModelEvent(this, new Object[] { oldRoot });
        for (TreeModelListener tml: treeModelListeners) {
            tml.treeStructureChanged(e);
        }
    }


    //////////////// TreeModel interface implementation ///////////////////////

    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.addElement(l);
    }

    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild(Object parent, int index) {
        Klass p = (Klass)parent;
        if (showAncestors) {

            return p.getParent();
        }
        return p.getChildAt(index);
    }

    /**
     * Returns the number of children of parent.
     */
    public int getChildCount(Object parent) {
        Klass p = (Klass)parent;
        if (showAncestors) {
            int count = 0;
            if (p.getParent() != null) {
                count++;
            }

            return count;
        }
        return p.getChildCount();
    }

    /**
     * Returns the index of child in parent.
     */
    public int getIndexOfChild(Object parent, Object child) {
        Klass p = (Klass)parent;
        if (showAncestors) {
            int count = 0;
            Klass father = p.getParent();
            if (father != null) {
                count++;
                if (father == child) {
                    return 0;
                }
            }

            return -1;
        }
        return p.getIndexOfChild((Klass)child);
    }

    /**
     * Returns the root of the tree.
     */
    public Object getRoot() {
        return rootKlass;
    }

    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf(Object node) {
        Klass p = (Klass)node;
        if (showAncestors) {
            return ((p.getParent() == null));
        }
        return p.getChildCount() == 0;
    }

    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }

    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged(TreePath path, Object newValue) {
        System.out.println("*** valueForPathChanged : " + path + " --> " + 
                           newValue);
    }

}
